<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="主频道" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelId">
              <j-dict-select-tag type="list" v-model="model.channelId" dictCode="mk_shop_channel ,name,id, parent_id = 0" placeholder="请选择主频道"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="商家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantId">
              <a-input v-model="model.name" placeholder="请选择商家"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessStd">
              <a-time-picker format="HH:mm" valueFormat="HH:mm" placeholder="请选择开始营业" :disabled="disabled"
                             v-model="model.businessStd" style="width:47%;" />
              &nbsp;至&nbsp;
              <a-time-picker style="width:47%;" format="HH:mm" valueFormat="HH:mm" :disabled="disabled"
                             placeholder="请选择结束营业" v-model="model.businessEnd" />
            </a-form-model-item>
          </a-col>
          <a-col :span="10">
            <a-form-model-item label="店铺logo" :labelCol="labelColUpload" :wrapperCol="wrapperColUpload" prop="logo">
              <j-image-upload v-model="model.logo"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="10">
            <a-form-model-item label="营业执照" :labelCol="labelColUpload" :wrapperCol="wrapperColUpload" style="width: none" prop="license">
              <j-image-upload v-model="model.license"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="佣金比例" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="commissionRate">
              <a-input v-model="model.commissionRate" placeholder="请输入佣金比例"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否休息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRest">
              <j-dict-select-tag type="list" v-model="model.isRest" dictCode="shop_rest" placeholder="请选择是否休息"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付宝姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zfbName">
              <a-input v-model="model.zfbName" placeholder="请输入支付宝姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付宝账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zfbAccount">
              <a-input v-model="model.zfbAccount" placeholder="请输入支付宝账户"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="省份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="province">
              <a-select v-model="model.province" placeholder="请选择省份" @change="getCity">
                <a-select-option v-for="(i) in provinceList" :value="i.sid" :key="i.sid">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city">
              <a-select v-model="model.city" placeholder="请选择城市" @change="getDistrict">
                <a-select-option v-for="(i) in cityList" :value="i.sid" :key="i.sid">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="区/县" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <a-select v-model="model.area" placeholder="请选择区/县">
                <a-select-option v-for="(i) in districtList" :value="i.sid" :key="i.sid">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ServeShopForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
  },
  data() {
    return {
      loaded: false,
      provinceList: [],
      cityList: [],
      districtList: [],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelColUpload: {
        xs: { span: 12 },
        sm: { span: 12 },
      },
      wrapperColUpload: {
        xs: { span: 12 },
        sm: { span: 8 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入门店名称!' },
        ],
        channelId: [
          { required: true, message: '请选择主频道!' },
        ],
        logo: [
          { required: true, message: '请上传logo!' },
        ],
        license: [
          { required: true, message: '请上传营业执照!' },
        ],
        commissionRate: [
          { required: true, message: '请输入佣金比例!' },
          {
              pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/,
              message: '请输入佣金比例!（1-0.01）'
          },
        ],
        isRest: [
          { required: true, message: '请选择是否休息!' },
        ],
        zfbName: [
          { required: true, message: '请输入支付宝姓名!' },
        ],
        zfbAccount: [
          { required: true, message: '请输入支付宝账户!' },
        ]
      },
      url: {
        add: '/shop/shopBasic/addForServe',
        edit: '/shop/shopBasic/edit',
        getProvince: '/shop/shopBasic/selectAreaProvinceList',
        getCity: '/shop/shopBasic/selectAreaCityList',
        getDistrict: '/shop/shopBasic/selectAreaDistrictList',
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    this.getProvince()
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
  },
  methods: {
    getProvince() {
      getAction(this.url.getProvince).then(res => {
        if (res.success) this.provinceList = res.result
      })
    },
    getCity(e) {
      // 清除选中
      this.model.city = null
      this.districtList = []
      this.model.area = null
      getAction(this.url.getCity + '/' + e).then(res => {
        if (res.success) this.cityList = res.result
      })
    },
    getDistrict(e) {
      this.model.area = null
      getAction(this.url.getDistrict + '/' + e).then(res => {
        if (res.success) this.districtList = res.result
      })
    },
    add() {
      this.getProvince()
      this.model = Object.assign({}, this.modelDefault)
      this.visible = true
    },
    edit(record) {
      console.log()
      this.getProvince()
      this.getCity(record.province)
      this.getDistrict(record.city)
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            this.model.type = 2
            method = 'post'
          } else {
            httpurl += this.url.edit
            this.model.type = 2
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
  }
}
</script>

<style scoped>
  .avatar-uploader > .ant-upload {
    width:104px;
    height:104px;
  }
</style>